package com.tzy.rpc.entity;/**
 * @description:
 * @Author HP
 * @create 2022/3/8 14:57
 */

import com.tzy.rpc.enumeration.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author HP
 * @create 2022-03-08 14:57
 * @desc
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RpcResponse<T> implements Serializable {
    /**
     * 响应对应的请求号
     */
    private String requestId;
    /**
     * 响应状态码
     */
    private Integer status;
    /**
     * 状态码对应的信息
     */
    private String msg;
    /**
     * 成功时的响应数据
     */
    private T data;

    /**
     * @description: 成功时服务器返回的对象
     * @param data
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> success(T data,String requestId){
        final RpcResponse<T> response = new RpcResponse<>();
        response.setStatus(ResponseCode.SUCCESS.getCode());
        response.setData(data);
        response.setRequestId(requestId);
        return response;
    }

    /**
     * @description: 失败时服务端返回的对象
     * @param code
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> fail(ResponseCode code){
        final RpcResponse<T> response = new RpcResponse<>();
        response.setStatus(code.getCode());
        response.setMsg(code.getMsg());
        return response;
    }

}
